/*
 * pthread_mutex_f.cpp
 *
 *  Created on: Aug 14, 2012
 *      Author: lucid
 */

#include "comm_lib_f.h"

#include "mutex_f.h"


mutexf_t::mutexf_t()
{
	pthread_mutex_init(&_mtx,0);
}

mutexf_t::~mutexf_t()
{
	pthread_mutex_destroy(&_mtx);
}

int mutexf_t::lock()
{
	return pthread_mutex_lock(&_mtx);
}

int mutexf_t::timedlock(const struct timespec *abstime)
{
	return pthread_mutex_timedlock(&_mtx,abstime);
}

int mutexf_t::unlock()
{
	return pthread_mutex_unlock(&_mtx);
}

int mutexf_t::trylock()
{
	return pthread_mutex_trylock(&_mtx);
}

int mutexf_t::is_locked()
{
	int iRet = pthread_mutex_trylock(&_mtx);
	if ( 0 == iRet )
	{
		pthread_mutex_unlock(&_mtx);
		return RETURN_ERROR_F;
	}
	else
	{
		return RETURN_SUCCESS_F;
	}
}

mutexf_t::operator pthread_mutex_t&()
{
	return _mtx;
}



mutexlf_t::mutexlf_t(pthread_mutex_t& m)
:_mtx(m)
{
	pthread_mutex_lock(&_mtx);
}

mutexlf_t::~mutexlf_t()
{
	pthread_mutex_unlock(&_mtx);
}










